'''
https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/?envType=study-plan-v2&envId=top-interview-150
'''
from typing import List


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        def build(l, r):
            if l > r:
                return None
            m = l + r >> 1
            root = TreeNode(nums[m])
            root.left = build(l, m-1)
            root.right = build(m+1, r)
            return root
        return build(0, len(nums)-1)